package com.sensePlusVR.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;
/**
 * 
 * @author: Loren
 * @create: 2016-7-20上午11:30:08
 * @desc: 获取屏幕分辨率
 */
@SuppressLint("InlinedApi")
@SuppressWarnings("deprecation")
public class AppUtil {
    
	public static int[] getScreenDispaly(Context context) {
    	WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		int width = windowManager.getDefaultDisplay().getWidth();// 手机屏幕的宽度
		int height = windowManager.getDefaultDisplay().getHeight();// 手机屏幕的高度
		int result[] = { width, height };
		return result;
	}
    
	/**
	 * Activity隐藏Android底部的虚拟按键
	 */
	public static void hideVirtualKey(Activity context){
		Window window = context.getWindow();
		WindowManager.LayoutParams params = window.getAttributes();
		params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
		window.setAttributes(params);
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
		
	}
	
	/**
	 * Dialog隐藏Android底部的虚拟按键
	 */
	public static void hideVirtualKey(Dialog context){
		Window window = context.getWindow();
		WindowManager.LayoutParams params = window.getAttributes();
		params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
		window.setAttributes(params);
		window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
		
	}
	
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
	public static boolean isNavigationBarShow(Activity context){
	    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
	        Display display = context.getWindowManager().getDefaultDisplay();
	        Point size = new Point();
	        Point realSize = new Point();
	        display.getSize(size);
	        display.getRealSize(realSize);
	        return realSize.y!=size.y;
	    }else {
	        boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
	        boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
	        if(menu || back) {
	            return false;
	        }else {
	            return true;
	        }
	    }
	}
	
	public static int getNavigationBarHeight(Activity activity) {
	    if (!isNavigationBarShow(activity)){
	        return 0;
	    }
	    Resources resources = activity.getResources();
	    int resourceId = resources.getIdentifier("navigation_bar_height",
	            "dimen", "android");
	    //获取NavigationBar的高度
	    int height = resources.getDimensionPixelSize(resourceId);
	    return height;
	}
}
